package com.bobo.web.provider.client.dingding;

import com.bobo.web.api.vo.alert.AlertParam;
import com.bobo.web.api.vo.alert.TextParam;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * Created by v on 2018/10/19.
 */
@Component
public class DingDingWarnClient {
    private static final Logger LOG = LoggerFactory.getLogger(DingDingWarnClient.class);
    private ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    private static String DD_URL = "https://oapi.dingtalk.com/robot/send?access_token=8d7ca06f4c9453e991f74031159a3ef761f6e578ffb62b65b5c6da20d0ff41ed";
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private Environment environment;


    public void sendAssignStudentDingDingMsg(Long cltId, Long studentId,String error) {
        try {
            AlertParam param = new AlertParam();
            TextParam text = new TextParam();
            String content = String.format("分配学生异常！学生ID=%s, lpId=%s  异常消息：%s", studentId, cltId , error);
            text.setContent(content);
            param.setMsgtype("text");
            param.setText(text);
            restTemplate.postForEntity(DD_URL, param, String.class);
        } catch (Exception e) {
            LOG.error("叮叮报警异常！", e);
        }
    }
    public void sendDingDingMsg(String DD_URL, String content) {
        try {
            AlertParam param = new AlertParam();
            TextParam text = new TextParam();
            text.setContent(content);
            param.setMsgtype("text");
            param.setText(text);
            restTemplate.postForEntity(DD_URL, param, String.class);
        } catch (Exception e) {
            LOG.error("叮叮报警异常！", e);
        }
    }
    public void sendAssignStudentReport(String msg) {
        try {
            AlertParam param = new AlertParam();
            TextParam textParam = new TextParam();
            String text = Arrays.toString(environment.getActiveProfiles()) + "环境，" +
                    msg+ "\n" ;
            textParam.setContent(text);
            param.setMsgtype("text");
            param.setText(textParam);
            restTemplate.postForEntity(DD_URL, param, String.class);
        } catch (Exception e) {
            LOG.error("叮叮报警异常！", e);
        }
    }

}
